/**
* This file is part of OntoCompoFramework
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package ontocompo.ui.appmanagement;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ontocompo.ui.global.Resources;

//import ontocompo.ui.OntoCompoLauncher;

public class ApplicationManager extends Observable implements ActionListener, Observer/*, ListSelectionListener*/ {

//	private OntoCompoLauncher ocl;
	private ArrayList<Application> availableApplications;
//	private ArrayList<Application> loadedApplications;
	private Application currentApplication;
	private Popup popup;
	private JComboBox choices;
	
	public ApplicationManager() {
//		this.ocl = ocl;
//		this.loadedApplications = new ArrayList<Application>();
	}
	
	public void setAvailableApplications(ArrayList<Application> availableApplications) {
		this.availableApplications = availableApplications;
	}
	
	public Application getCurrentApplication() {
		return this.currentApplication;
	}
	
/*	public ArrayList<Application> getLoadedApplications() {
		return this.loadedApplications;
	}*/
	
	public void setCurrentApplication(Application currentApplication) {
		this.currentApplication = currentApplication;
//		this.currentApplication.setExtractor(this.ocl.getExtractor());
//		this.ocl.getExtractor().setCurrentApplication(this.currentApplication);
//		this.ocl.setApplicationNameLabel(this.currentApplication.getName());
//		this.ocl.setApplicationViewPaneTabbedPane(this.currentApplication.getUiViewPanel(), this.currentApplication.getTaskViewPanel(), this.currentApplication.getComponentViewPanel());
		
		this.setChanged();
		this.notifyObservers(this.currentApplication);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
//		this.ocl.setEnabled(false);
		
		PopupFactory pf = new PopupFactory();
		this.popup = pf.getPopup(null, this.getPopUpPanel(), java.awt.Toolkit.getDefaultToolkit().getScreenSize().width/2, java.awt.Toolkit.getDefaultToolkit().getScreenSize().height/2);
		this.popup.show();
	}
	
	private JPanel getPopUpPanel() {
		JPanel jpanel = new JPanel();
		jpanel.setBackground(Color.WHITE);
		jpanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		jpanel.setLayout(new BorderLayout());
		
		JLabel jlabel = new JLabel("Select an application to load:");
		jpanel.add(jlabel, BorderLayout.NORTH);
		
		this.choices = new JComboBox();
		for(Application app: this.availableApplications) {
			this.choices.addItem(app);
		}
		jpanel.add(this.choices, BorderLayout.CENTER);
		
		JPanel buttonsPanel = new JPanel();
		buttonsPanel.setBackground(Color.white);
		buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				cancelAction();
			}
			
		});
		
		JButton loadButton = new JButton("Load");
		loadButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				loadAction();
			}
			
		});
		
		buttonsPanel.add(cancelButton);
		buttonsPanel.add(loadButton);
		jpanel.add(buttonsPanel, BorderLayout.SOUTH);
		//this.popup.hide();
		return jpanel;
	}
	
	public void cancelAction() {
		this.popup.hide();
//		this.ocl.setEnabled(true);
	}
	
	public void loadAction() {
		Application app = (Application) this.choices.getSelectedItem();
		this.loadApplication(app);
		this.popup.hide();
	}
	
	public void loadApplication(Application app) {
		if(app != null) {
			this.setCurrentApplication(app);
			Resources.LOADEDAPPLICATIONS.add(app);
			this.availableApplications.remove(app);
			
			app.addObserver(this);
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		Application currentApp = (Application) o;
		this.setCurrentApplication(currentApp);
	}
	
//	@Override
//	public void valueChanged(ListSelectionEvent e) {
//		// TODO Auto-generated method stub
//		if (e.getValueIsAdjusting() == false) {
//	        if (this.ocl.getApplicationList().getSelectedIndex() != -1) {
//	        	this.setCurrentApplication((Application) this.ocl.getApplicationList().getSelectedValue());
//	        }
//	    }
//	}
	
	public String getUriJComponent(Component jc) {
		for(Application app: Resources.LOADEDAPPLICATIONS) {
			String jcUri = app.getComponentUid(jc);
			if(jcUri != null) {
				return jcUri;
			}
		}
		
		return null;
	}
	
	public Application getApplicationOfJComponentUri(String jcUri) {
		for(Application app: Resources.LOADEDAPPLICATIONS) {
			JComponent jc = app.getJComponentFromComponentUid(jcUri);
			
			if(jc != null) {
				return app;
			}
		}
		
		return null;
	}
}
